import scrapy
from Hue.basepro import ZhengFuBaseSpider


class XiningSpider(ZhengFuBaseSpider):
    name = 'Xining-pro'
    allowed_domains = ['xining.gov.cn']
    start_urls = ['http://http://www.xining.gov.cn//']
    api = 'http://www.xining.gov.cn/igs/front/search.jhtml?code=76fc023bff5b4e65bcf48c8b08da897d&timeOrder=&siteId=2&WCMSITEID=3&orderBy=&position=&orderDirection=&orderProperty=&searchWord={keyword}&pageSize=10&type=65,68,66,70&pageNum={page}'
    keywords = ['煤炭']
    method = 'GET'

    def edit_page(self, response):
        raw_data = response.json()
        all_page = raw_data["page"]["totalPages"]
        return int(all_page)

    def edit_items_box(self, response):
        raw_data = response.json()
        items_box = raw_data["page"]["content"]
        return items_box

    def edit_item(self, item):
        meta_info = {}
        meta_info["title"] = item["DOCTITLE"]
        meta_info["url"] = item["url"]
        meta_info["pre_content"] =  item["DOCCONTENT"]
        try:
            meta_info['date'] =  item['PUBDATEformat']
        except:
            pass

        return meta_info
